/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainMenu.java
 *
 * Created on Dec 4, 2011, 3:22:21 PM
 */
/**
 * 
 * @author Ahmed
 */

public class MainMenu extends javax.swing.JFrame {

	/** Creates new form MainMenu */
	public MainMenu() {
		initComponents();
	}
	
	private String[] query={""};
	private String[]tables;
	private Object[][]table;
	private Object[][]finalTable;
	private String []columnNames;
	public String[] getQuery() {
		return query;
	}
	
	
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        queryField = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        text = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        addTable = new javax.swing.JToggleButton();
        createSchema = new javax.swing.JToggleButton();
        deleteTable = new javax.swing.JComboBox();
        addColumn = new javax.swing.JComboBox();
        deleteSchema = new javax.swing.JToggleButton();
        jDesktopPane2 = new javax.swing.JDesktopPane();
        view = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane2 = new javax.swing.JScrollPane();
        schemaTables = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        scrollTable = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        runButton = new javax.swing.JToggleButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        fileButton = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        editButton = new javax.swing.JMenu();
        copyButton = new javax.swing.JMenuItem();
        cutButton = new javax.swing.JMenuItem();
        pastButton = new javax.swing.JMenuItem();
        runButton2 = new javax.swing.JMenu();
        viewMenu = new javax.swing.JMenu();
        helpMenu = new javax.swing.JMenu();
        instructions = new javax.swing.JMenuItem();
        aboutUs = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Main Menu");
        setMinimumSize(new java.awt.Dimension(500, 500));
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        setName("MainMenu"); // NOI18N

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 204));
        jScrollPane1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(51, 51, 51), 1, true));

        queryField.setColumns(100);
        queryField.setFont(new java.awt.Font("Arial", 0, 13));
        queryField.setRows(5);
        queryField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                queryFieldKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(queryField);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 153, 0));
        jLabel1.setText("Enter the query here :");

        text.setFont(new java.awt.Font("Tahoma", 1, 14));
        text.setForeground(new java.awt.Color(255, 255, 255));
        text.setText("Actions:");
        text.setBounds(10, 10, 110, 20);
        jDesktopPane1.add(text, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator4.setBounds(10, 32, 110, 10);
        jDesktopPane1.add(jSeparator4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        addTable.setBackground(new java.awt.Color(255, 204, 153));
        addTable.setText("Add Table");
        addTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTableActionPerformed(evt);
            }
        });
        addTable.setBounds(10, 130, 130, 23);
        jDesktopPane1.add(addTable, javax.swing.JLayeredPane.DEFAULT_LAYER);

        createSchema.setBackground(new java.awt.Color(255, 204, 153));
        createSchema.setText("Create Schema");
        createSchema.setBounds(10, 50, 130, 23);
        jDesktopPane1.add(createSchema, javax.swing.JLayeredPane.DEFAULT_LAYER);

        deleteTable.setBackground(new java.awt.Color(153, 255, 153));
        deleteTable.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Delete table" }));
        deleteTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteTableActionPerformed(evt);
            }
        });
        deleteTable.setBounds(10, 250, 130, 20);
        jDesktopPane1.add(deleteTable, javax.swing.JLayeredPane.DEFAULT_LAYER);

        addColumn.setBackground(new java.awt.Color(153, 255, 153));
        addColumn.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Add Column", "Item 2", "Item 3", "Item 4" }));
        addColumn.setBounds(10, 210, 130, 20);
        jDesktopPane1.add(addColumn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        deleteSchema.setBackground(new java.awt.Color(255, 204, 153));
        deleteSchema.setText("Delete Schema");
        deleteSchema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteSchemaActionPerformed(evt);
            }
        });
        deleteSchema.setBounds(10, 90, 130, 23);
        jDesktopPane1.add(deleteSchema, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jDesktopPane2.setForeground(new java.awt.Color(204, 204, 0));

        view.setForeground(new java.awt.Color(0, 0, 102));
        view.setFont(new java.awt.Font("Tahoma", 1, 12));

        jPanel1.setBackground(new java.awt.Color(255, 255, 204));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Tables :");

        schemaTables.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(schemaTables);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(2, 2, 2)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addContainerGap())
        );

        view.addTab("My Schema", jPanel1);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(finalTable,columnNames));
        scrollTable.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollTable, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollTable, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
        );

        view.addTab("View", jPanel2);

        view.setBounds(10, 10, 710, 370);
        jDesktopPane2.add(view, javax.swing.JLayeredPane.MODAL_LAYER);

        runButton.setBackground(new java.awt.Color(204, 51, 0));
        runButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        runButton.setText("RUN");
        runButton.setFocusable(false);
        runButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        runButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButtonActionPerformed(evt);
            }
        });

        fileButton.setText("File");

        jMenuItem2.setText("New Schema");
        fileButton.add(jMenuItem2);

        jMenuItem1.setText("Exit");
        fileButton.add(jMenuItem1);

        jMenuBar1.add(fileButton);

        editButton.setText("Edit");

        copyButton.setText("Copy");
        editButton.add(copyButton);

        cutButton.setText("Cut");
        editButton.add(cutButton);

        pastButton.setText("Past");
        editButton.add(pastButton);

        jMenuBar1.add(editButton);

        runButton2.setText("Run");
//      runButton2.addMouseListener(new java.awt.event.MouseAdapter() {
//          public void mousePressed(java.awt.event.MouseEvent evt) {
//              runButton2MousePressed(evt);
//          }
//      });
        runButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButton2ActionPerformed(evt);
            }
        });
        jMenuBar1.add(runButton2);

        viewMenu.setText("View");
        jMenuBar1.add(viewMenu);

        helpMenu.setText("Help");

        instructions.setText("Instructions");
        helpMenu.add(instructions);

        aboutUs.setText("About");
        helpMenu.add(aboutUs);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(runButton, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 742, Short.MAX_VALUE)
                    .addComponent(jDesktopPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 742, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(72, 72, 72)
                                .addComponent(runButton, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDesktopPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE)
                    .addComponent(jDesktopPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

//	private void runButton2MousePressed(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_runButton2MousePressed
//		// TODO add your handling code here:
//		Validation validate = new Validation(query);
//		try {
//			if (!validate.validate()) {
//				queryField.setText("INVALID INPUT !!!");
//			} else {
//				System.out.println("VALID");
//			}
//		} catch (Exception e) {
//			queryField.setText("Please,Enter a query !!!");
//		}
//	}// GEN-LAST:event_runButton2MousePressed

	private void deleteSchemaActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_deleteSchemaActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_deleteSchemaActionPerformed

	private void runButton2ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_runButton2ActionPerformed
		// TODO add your handling code here:
		Validation validate = new Validation(query);
		try {
			if (!validate.finalParsing()) {
				queryField.setText("INVALID INPUT !!!");
			} else {
				System.out.println("VALID");
			}
		} catch (Exception e) {
			queryField.setText("Please,Enter a valid query !!!");
		}
	}// GEN-LAST:event_runButton2ActionPerformed

	private void addTableActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addTableActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_addTableActionPerformed

	private void deleteTableActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jComboBox1ActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jComboBox1ActionPerformed

	private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_runButtonActionPerformed
		for(int i=0;i<query.length;i++){
			System.out.print(query[i]+" ");
		}
		Validation validate = new Validation(query);
			try {
				if (!validate.finalParsing()) {
					queryField.setText("INVALID INPUT !!!");
				} else {
					System.out.println("VALID");
				}
			} catch (Exception e) {
				queryField.setText("Please,Enter a valid query !!!");
			}
		// System.out.println(validate.validate(query)+" "+validate.getSQL());
	}// GEN-LAST:event_runButtonActionPerformed

	private void queryFieldKeyPressed(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_queryFieldKeyPressed
		int key = evt.getKeyCode();
		int linesCounter = 0;
		if (key == 10) {
			int linesNum = 0;
			String linesDate = queryField.getText() + "\n";
			for (int j = 0; j < linesDate.length(); j++) {
				if (linesDate.charAt(j) == 10) {
					linesNum++;
				}
			}
			query = new String[linesNum];
			int from = -1, to = 0;
			for (int i = 0; i < linesDate.length(); i++) {
				if (linesDate.charAt(i) == 10) {
					to = i;
					query[linesCounter] = linesDate.substring(from + 1, to);
					from = to;
					linesCounter++;
				}
			}
			for (int i = 0; i < query.length; i++) {
				// for(int j=0;j<query[i].length();j++){
				// System.out.print(query[i].charAt(j)+0+" ");}
				System.out.println(query[i]);
			}
			System.out.println("==================================");
		}
	}// GEN-LAST:event_queryFieldKeyPressed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		int[][] input = { {1,2,3}, {4,5,6 },{7,8,9},{10,11,12}};
		System.out.println(input.length+" "+input[0].length);
		int output[][] = new int[input[0].length][input.length];
		int counter1 = 0;
		int counter2 = 0;
		System.out.println();
		for (int i = 0; i < output.length; i++) {
			for (int j = 0; j <output[i].length; j++) {
				output[counter2][counter1] =input[j][i];
				counter1++;
			}
			counter2++;
			counter1 = 0;
		}
		counter2 = 0;
		for(int i=0;i<output.length;i++){
			for(int j=0;j<output[i].length;j++){
				System.out.print(output[i][j]+" ");
			}System.out.println();
		}
		
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new MainMenu().setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutUs;
    private javax.swing.JComboBox addColumn;
    private javax.swing.JToggleButton addTable;
    private javax.swing.JMenuItem copyButton;
    private javax.swing.JToggleButton createSchema;
    private javax.swing.JMenuItem cutButton;
    private javax.swing.JToggleButton deleteSchema;
    private javax.swing.JComboBox deleteTable;
    private javax.swing.JMenu editButton;
    private javax.swing.JMenu fileButton;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuItem instructions;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JDesktopPane jDesktopPane2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JTable jTable1;
    private javax.swing.JMenuItem pastButton;
    private javax.swing.JTextArea queryField;
    private javax.swing.JToggleButton runButton;
    private javax.swing.JMenu runButton2;
    private javax.swing.JList schemaTables;
    private javax.swing.JScrollPane scrollTable;
    private javax.swing.JLabel text;
    private javax.swing.JTabbedPane view;
    private javax.swing.JMenu viewMenu;
    // End of variables declaration//GEN-END:variables
}
